package com.linkoog.devtools.generate.singleton.builder;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;

/**
 */
public abstract class BaseBuilder {
    public void build(@NotNull Project project,@NotNull Editor editor,@NotNull PsiClass psiClass) {
        WriteCommandAction.runWriteCommandAction(project, () -> {
            String className = psiClass.getNameIdentifier().getText();
            PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
            build(editor, elementFactory, project, psiClass, className);
        });
    }

    public abstract void build(Editor editor, PsiElementFactory elementFactory, Project project, PsiClass psiClass, String className);

    protected boolean containFiled(PsiClass psiClass, PsiField psiField) {
        return psiClass.findFieldByName(psiField.getName(), true) != null;
    }

    protected boolean containMethod(PsiClass psiClass, PsiMethod psiMethod) {
        return psiClass.findMethodsByName(psiMethod.getName(), true).length > 0;
    }

    protected boolean containClass(PsiClass psiClass, PsiClass innerClass) {
        return psiClass.findInnerClassByName(innerClass.getName(), true) != null;
    }
}
